.
.Mine Sweeper by Craig 'preppie' Miller
.

.
. Setup variables and PM graphics
.

o=960
b=128
pmgraphics 1
n=pmadr(0)
y=b
mset n+y,8,15
data f() word = -41,-40,-39,-1,1,39,40,41,0
w=dpeek(88)
dim a(o) byte,c(o),d(o)byte
m=50

.
. Main Loop
.

do
.
. Select Mines
.
	poke 752,1
	q=adr(d)
	mset q,o,b
	mset w,o,0
	while strig(0)
		s=stick(0)
		m=m+(s=14)*(m<99)-(s=13)*(m>10)
		pos.0,0
		?"mine#";m;
		pause 2
	wend
.
. Draw Screen
.

	for i=81 to 920 step 40
		mset w+i,38,b
		mset q+i,38,0
	next
	move q,adr(a),o
	i=0
	
.
. Hide the mines
.

	repeat
		h=rand(38)+rand(21)*40+81
		if a(h)=0
			a(h)=9
			inc i
		endif
	until i=m
	l=m
	q=w+81
	v=0
.
. Find out how many mines surround each square
.

	for i=81 to o
		pos.0,0
		?o-i;" ";
		if a(i)=0
			for h=0 to 7
				a(i)=a(i)+(a(i-f(h))=9)
			next
		endif
	next
	g=798
	x=72
	r=0
	z=time
	
.
. Main game loop
.

	repeat
		poke 704,55
		pos.0,0
		p=peek(q)

.
. Get player input
.

		s=stick(0)
		t=strig(0)
		k=key()
		if k>0
			get k
			if p=159
				poke q,b
				inc m
			elif p=b and m
				poke q,159
				dec m
			endif
		elif t
			x=x+(s&8=0)*4*(x<196)-(s&4=0)*4*(x>48)
			mset n+y,8,0
			y=y+(s&2=0)*8*(y<204)-(s&1=0)*8*(y>48)
			q=w+(y-32)/8*40+(x-44)/4
		else
.
. This uncovers the squares but I'm too short
. on time to explain it :)
.

			r=a(q-w)
			if r=0
				poke 704,1
				j=1
				c(1)=q-w
				while j
					u=c(j)
					if u
						for i=0 to 8
							h=u-f(i)
							e=peek(h+w)
							k=a(h)
							if k=0 and d(h)=0
								c(j)=h
								d(h)=1
								inc j
							elif d(h)<2
								g=g-(e>127)
								m=m+(e=159)
								d(h)=2
								poke h+w,(k+(k<>0)*16)*(k<>b)
							endif
						next
					endif
					dec j
				wend
			elif r<>9 and p>=b
				poke q,r+16
				dec g
			endif
		endif
		i=time
		if i<z
			z=i
		endif
		if i-z>50
			z=i
			inc v
		endif
		mset n+y,8,15
		pmhpos 0,x
		pause 3
		?"Time=";v;" Mines=";m;" "
	until v>9998 or r=9 or g=l

.
. endgame stuff
.

	for i=41 to o
		if a(i)=9
			poke w+i,10
		endif
	next
	if g=l
		?"won!score=";v
	else
		?"lost";
	endif
	while strig(0)
	wend
	cls
	pause 9
loop